package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.model.domain.Question;
import com.tanhua.model.domain.Settings;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SettingsService {

    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private SettingsApi settingsApi;

    @DubboReference
    private BlackListApi blackListApi;

    // 查询用户通用设置
    public SettingsVo settings() {
        SettingsVo vo = new SettingsVo();

        // 1. 查询用户id
        Long userId = UserHolder.getUserId();
        vo.setId(userId);

        // 2. 查询用户手机号
        vo.setPhone(UserHolder.getMobile());

        // 3. 查询用户陌生人问题
        Question question = questionApi.findByUserId(userId);
        // 设置默认问题
        String txt = question == null ? "你喜欢Java吗?" : question.getTxt();
        vo.setStrangerQuestion(txt);

        // 4. APP通知开关数据
        Settings settings = settingsApi.findByUserId(userId);
        if(settings != null) {
            vo.setGonggaoNotification(settings.getGonggaoNotification());
            vo.setLikeNotification(settings.getLikeNotification());
            vo.setPinglunNotification(settings.getPinglunNotification());
        }

        return vo;
    }

    // 设置陌生人问题
    public void saveQuestions(String content) {
        // 1. 获取用户id
        Long userId = UserHolder.getUserId();

        // 2. 调用api查询用户陌生人问题
        Question question = questionApi.findByUserId(userId);

        // 3. 判断问题是否存在
        if(question == null) {
            // 3.1 如果不存在,保存
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            questionApi.save(question);

        } else {
            // 3.2 如果存在,更新
            question.setTxt(content);
            questionApi.update(question);
        }
    }

    // 通知设置
    public void saveSettings(Map map) {
        Boolean likeNotification = (Boolean) map.get("likeNotification");
        Boolean pinglunNotification = (Boolean) map.get("pinglunNotification");
        Boolean gonggaoNotification = (Boolean) map.get("gonggaoNotification");

        // 1. 获取用户id
        Long userId = UserHolder.getUserId();

        // 2. 调用api查询用户通知设置
        Settings settings = settingsApi.findByUserId(userId);

        // 3. 判断通知设置是否存在
        if(settings == null) {
            // 4.1 如果不存在,保存
            settings = new Settings();
            settings.setUserId(userId);
            settings.setLikeNotification(likeNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setPinglunNotification(pinglunNotification);
            settingsApi.save(settings);

        } else {
            // 4.2 如果存在,更新
            settings.setLikeNotification(likeNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setPinglunNotification(pinglunNotification);
            settingsApi.update(settings);
        }
    }

    // 分页查询黑名单
    public PageResult blacklist(int page, int size) {
        // 1. 获取当前用户id
        Long userId = UserHolder.getUserId();

        // 2. 调用API查询用户黑名单分页列表  IPage对象
        IPage<UserInfo> iPage = blackListApi.findByUserId(userId, page, size);

        // 3. 对象转化,将查询的IPage对象的内容封装到PageResult中
        PageResult pr = new PageResult(page, size, iPage.getTotal(), iPage.getRecords());

        // 4. 返回
        return pr;
    }

    // 取消黑名单
    public void deleteBlackList(Long blackUserId) {
        // 1. 获取当前用户id
        Long userId = UserHolder.getUserId();

        // 2. 调用api删除
        blackListApi.delete(userId, blackUserId);
    }
}
